package projet;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

/**
 * Tools: Functions that may be use to help !
 * 
 * @author CORSINI Pierre, Cyril CHANDELIER, Sylvain BRUN
 * @version January 25, 2010
 */
public class Tools {
	public static Scanner console = new Scanner(System.in);

	/**
	 * Encode using MD5 encryption
	 * @param The input string
	 * @return The input string encoded
	 */
	public static String Encode(String s) {
		byte[] uniqueKey = s.getBytes();
		byte[] hash = null;
		try {
			hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
		} catch (NoSuchAlgorithmException e) {
			// Yea, we know MD5 exists...
		}
		StringBuilder hashString = new StringBuilder();
		for (int i = 0; i < hash.length; i++) {
			String hex = Integer.toHexString(hash[i]);
			if (hex.length() == 1) {
				hashString.append('0');
				hashString.append(hex.charAt(hex.length() - 1));
			} else
				hashString.append(hex.substring(hex.length() - 2));
		}
		return hashString.toString();
	}

	/**
	 * Delete the file at the given path
	 * 
	 * @param path The path
	 * @return True if success
	 */
	public static Boolean deleteFile(String path) {
		try {
			return new File(path).delete();
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * Get the execution path of the current application
	 * @return
	 */
	public static String getExecutionPath() {
		try {
			return System.getProperty("user.dir");
		} catch (Exception e) {
			return "?";
		}

	}

	/**
	 * Print with a shift of shift blanks
	 * 
	 * @param shift The offset
	 * @param o The object to print
	 */
	public static void print(int shift, Object o) {
		System.out.println(Tools.shiftText(shift, o.toString()));
	}

	/**
	 * Print with no shift
	 * 
	 * @param o The object to print
	 */
	public static void print(Object o) {
		Tools.print(0, o);
	}
	
	/**
	 * Print a blank
	 */
	public static void print()
	{
		Tools.print(0,"");
	}

	/**
	 * Print with shift, using err output
	 * 
	 * @param shift
	 * @param o The object to print
	 */
	public static void printerr(int shift, Object o) {
		System.err.println("Error : " + Tools.shiftText(shift, o.toString()));
	}

	/**
	 *  Print with no shift, using err output
	 * 
	 * @param o The object to print
	 */
	public static void printerr(Object o) {
		Tools.printerr(0, o);
	}

	/**
	 * Read the content of a file
	 * 
	 * @param f The File
	 * @return A string containing the file's content
	 * @throws IOException
	 */
	public static String readFile(File f) throws IOException {
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String ret = "", nextLine;
		while ((nextLine = br.readLine()) != null)
			ret += nextLine + "\n";

		fr.close();
		br.close();
		return ret;
	}

	/**
	 * Read the standard input (line)
	 * 
	 * @return The String read
	 */
	public static String readLine() {
		System.out.print('>');
		return Tools.console.nextLine();
	}

	/**
	 * Read the standard input (line), with a given old value
	 * 
	 * @param oldValue
	 * @return The String read, that can be oldvalue if no input
	 */
	public static String readLineEdit(Integer oldValue) {
		if (oldValue == null)
			return Tools.readLine();
		else
			return Tools.readLineEdit(oldValue.toString());
	}

	/**
	 * Read the standard input (line), with a given old value
	 * 
	 * @param oldValue
	 * @return The String read, that can be oldvalue if no input
	 */
	public static String readLineEdit(String oldValue) {
		if (oldValue == null || oldValue == "")
			return Tools.readLine();

		System.out.print("[OldValue: " + oldValue + "](Enter to pass) >");
		String buff = Tools.console.nextLine();
		if (buff.length() == 0)
			return oldValue;
		else
			return buff;
	}

	/**
	 * Shift a text to x blank
	 * 
	 * @param shift Number of blank
	 * @param msg The text
	 * @return The shifted text
	 */
	public static String shiftText(int shift, String msg) {
		String blank = "";
		for (int i = 0; i < shift; i++)
			blank += " ";

		msg = msg.replaceAll("\n", "\n" + blank);
		msg = blank + msg;
		return msg;

	}

	/**
	 * Write a String in a file (override)
	 * 
	 * @param f The File
	 * @param s The String
	 * @return True if succeed
	 */
	public static Boolean writeFile(File f, String s) {
		try {
			FileWriter fr = new FileWriter(f);
			BufferedWriter bw = new BufferedWriter(fr);
			bw.write(s);
			bw.close();
			fr.close();
		} catch (IOException e) {
			return false;
		}
		return true;
	}

	/**
	 * Return the given string with a upper case first char
	 * @param s
	 * @return the resulting string
	 */
	public static String firstLetterUp(String s) {
		if (s.length() < 1 || s == null)
			return s;
		else
			return Character.toUpperCase(s.charAt(0)) + s.substring(1);
	}

}
